package com.ellricky.teacher.ui.mycourse.evaluate

import android.content.Intent
import android.view.View
import com.andr.common.http.callback.JOCallback
import com.andr.common.ui.activity.BaseToolBarActivity
import com.andr.common.ui.mvp.BasePresenter
import com.andr.common.util.EmptyUtil
import com.andr.common.util.ToastUtil
import com.ellricky.baselib.BaseConstant
import com.ellricky.baselib.dialog.picker.EPickerEntityListener
import com.ellricky.baselib.dialog.picker.EPickerType
import com.ellricky.baselib.dialog.picker.EPickerUtil
import com.ellricky.baselib.entity.IdNamePickerEntity
import com.ellricky.baselib.entity.PersonItemEntity
import com.ellricky.baselib.http.HttpUtils
import com.ellricky.baselib.util.EAppUtil
import com.ellricky.teacher.API_STU_EVALUATE
import com.ellricky.teacher.R
import com.ellricky.teacher.jumpStuEvaluate
import kotlinx.android.synthetic.main.tc_activity_attend_class_state.*
import org.json.JSONObject
import java.util.*

class AttendClassStateActivity : BaseToolBarActivity<Void, BasePresenter<Void>>(), EPickerEntityListener<IdNamePickerEntity> {

    private lateinit var student: PersonItemEntity

    private lateinit var ePickerUtil: EPickerUtil
    private val stateList = ArrayList<IdNamePickerEntity>()
    private var selState: IdNamePickerEntity? = null


    override fun getContentLayout(): Int {
        return R.layout.tc_activity_attend_class_state
    }

    override fun initUIView() {
        super.initUIView()
        mTvTitle.text = getString(R.string.tc_if_attend_class)

        ePickerUtil = EPickerUtil(this)
        stateList.add(IdNamePickerEntity("1", "未上课"))
        stateList.add(IdNamePickerEntity("2", "上课"))
        stateList.add(IdNamePickerEntity("3", "未完成"))

        student = intent.getSerializableExtra("student") as PersonItemEntity

        etState.setOnClickListener(this)
        btnNext.setOnClickListener(this)
    }


    override fun onClickView(v: View?) {
        super.onClickView(v)
        when (v) {
            etState -> ePickerUtil.showClassStatePicker(stateList, selState, this)
            btnNext -> postSubmit()
        }
    }


    override fun onSelectItem(pickType: Int, t: IdNamePickerEntity?) {
        val name = t?.name
        when (pickType) {
            EPickerType.PICK_CLASS_ATTEND_STATE -> {
                selState = t
                etState.value = name
            }
        }
    }


    private fun postSubmit() {
        val state = etState.value
        if (EmptyUtil.isEmpty(state)) {
            ToastUtil.show(this, R.string.tc_select_attend_state)
            return
        }

        when (val classStatus = selState?.id ?: "") {
            // 下一步
            "2" -> jumpStuEvaluate(this, student, classStatus)

            else -> {
                // 提交评估
                val url = EAppUtil.getApiHost() + API_STU_EVALUATE
                val paramMap = mapOf(
                        "customerId" to student.id,
                        "attendId" to student.attendId,
                        "classStatus" to classStatus
                )
                HttpUtils.getInstance().post(url, paramMap, object : JOCallback(this) {
                    override fun onSuccess(dataJO: JSONObject, msg: String) {
                        setResult(BaseConstant.RESULT_CODE_ADD)
                        finish()
                    }
                })


            }
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        setResult(resultCode)
        finish()
    }
}

